﻿Public Class frmchangelogin
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        frmmain.Dispose()
        Me.Dispose()
    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            If Me.TxtUser.Text.Trim = "" Then
                MsgBox("กรุณากรอก Username", MsgBoxStyle.Critical)
                Me.TxtUser.Focus()
                Exit Sub
            End If
            If Me.TxtPassword.Text.Trim = "" Then
                MsgBox("กรุณากรอก Password", MsgBoxStyle.Critical)
                Me.TxtPassword.Focus()
                Exit Sub
            End If
            Dim DS As New DataSet
            Strsql = "Select * from user left join siteuser on user.user = siteuser.user where user.user = '" & Me.TxtUser.Text.Trim & "' and user.password  = '" & Crypto.Encrypt(Me.TxtPassword.Text, "11266") & "' and siteuser.siteid = " & Me.CboSite.SelectedValue
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_user", "user")
            If DS.Tables(0).Rows.Count < 1 Then
                MsgBox("Username หรือ Password ไม่ถูกต้อง", MsgBoxStyle.Critical)
                Exit Sub
            Else

                ' frmmain.User = Me.TxtUser.Text
                frmmain.TxtUser.Text = DS.Tables(0).Rows(0).Item("user_name")
                frmmain.TxtSiteName.Text = "คลังวัสดุ : " & CboSite.Text.ToString
                frmmain.TxtComputername.Text = "Computer : " & Pn_Framework.Get_Computername
                My.Settings.CurrentuserGroup = DS.Tables(0).Rows(0).Item("usergroup")

                My.Settings.Currentuser = Me.TxtUser.Text
                My.Settings.CurrentSite = Me.CboSite.SelectedValue
                My.Settings.CurrentIPaddress = Pn_Framework.Get_IPAddress
                My.Settings.CurrentComputer = Pn_Framework.Get_Computername

                Strsql = "Select * from onlineuser where computername = '" & Pn_Framework.Get_Computername & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_onlineuser", "onlineuser")
                If DS.Tables(0).Rows.Count > 0 Then
                    Strsql = "Delete from onlineuser where computername = '" & Pn_Framework.Get_Computername & "'"
                    Mysql1.MySQLExecute(Strsql)
                End If
                Strsql = "Insert into onlineuser (user,computername,ipaddress,site,onlineid) values ('" & Me.TxtUser.Text.Trim & "','" & Pn_Framework.Get_Computername & "','" & Pn_Framework.Get_IPAddress & "','" & Me.CboSite.SelectedValue & "','" & Pn_Framework.Get_GUID & "')"
                Mysql1.MySQLExecute(Strsql)


            End If
            Me.Hide()
            frmmain.Show()
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Login - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmchangelogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ComboboxData_site()
    End Sub
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectedValue = My.Settings.CurrentSite
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
End Class